#pragma once
#include "os/socket.h"
#include "base/core/os/debug.h"
#include "base/core/double_list.h"

#ifdef _MSC_VER
#pragma warning(disable : 4201)
#endif

#define MAX_BUFF_SIZE  8*1024

extern char g_errstr[1024];
#define ERRNO   (Socket::GetSocketError())
#define ERRSTR  (Socket::GetSocketErrorMsg(ERRNO,  \
                 g_errstr,                   \
                 sizeof(g_errstr)))

enum PollType 
{
	POLL_TYPE_NULL  = 0,
	POLL_TYPE_READ  = 1,
	POLL_TYPE_WRITE = 2,
};

enum SocketFlag 
{
	SOCKET_LISTEN,
	SOCKET_ACCEPT,
	SOCKET_CONNECT,
};

enum SessionState 
{
	SESSION_STATE_NULL = 0,
	SESSION_STATE_WORKING,    // working
	SESSION_STATE_SHUTDOWN,   // app lay shutdown(grace close socket)
};

enum EventFlag 
{
	EVENT_NULL  = 0,
	EVENT_READ  = 1,
	EVENT_WRITE = 1 << 1,
	EVENT_RANDW = EVENT_READ | EVENT_WRITE,
};

struct EventData 
{
	void*       user_req;
	void*       context;
	void*       handle;
	void*       subpackage;
	void*       netpackhandle;
	SocketFlag  sock_flag;
	EventFlag   event_flag;
	union 
	{
		struct 
		{
			SOCKET   sock;
			uint16_t port;
		};
		void*        session;
	};

	EventData() 
	{
		sock_flag  = SOCKET_ACCEPT;
		event_flag = EVENT_NULL;
		sock       = INVALID_SOCKET;
		port       = 0;
		user_req   = NULL;
		context    = NULL;
		handle     = NULL;
		session    = NULL;
		netpackhandle = NULL;
	}
};

struct EventDataEx : public EventData 
{
    uint32_t  sendBufSize;
    uint32_t  recvBufSize;

    EventDataEx()
		: EventData()
		, sendBufSize(0)
		, recvBufSize(0) 
	{}
};
